package cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.service;

import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.AchievementType;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.entity.Patent;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.AbsAchievementMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.mapper.PatentMapper;
import cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.util.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PatentService {
    @Autowired
    private PatentMapper patentMapper;
    @Autowired
    private AbsAchievementMapper absAchievementMapper;
    @Autowired
    private SnowflakeIdGenerator idGenerator;


    @Transactional // 声明事务
    public boolean addPatent(Patent patent) {
        Long sharedId = idGenerator.generateId();
        patent.setAchievementType(new AchievementType(1L, "专利", "ZL"));

        // 1. 插入 Achievement 相关记录
        absAchievementMapper.addAchievement(sharedId, patent.getAchievementType());

        // 2. 插入 Patent 记录
        patent.setId(sharedId);
        return patentMapper.addPatent(patent);
    }

    // 通过删除Achievement表数据 级联删除专利和对应支撑材料

    public boolean deletePatent(Long patentId) {return absAchievementMapper.deleteById(patentId);}


    public boolean updatePatent(Patent patent) {
        System.out.println(patent);
        return patentMapper.updatePatent(patent);
    }



    public PageInfo<Patent> selectPatentsByCondition(Integer pageNum, Integer pageSize, Patent patent) {
        PageHelper.startPage(pageNum, pageSize);
        List<Patent> patentList = patentMapper.selectPatentsByCondition(patent);
        return new PageInfo<>(patentList);
    }

}
